package com.syqc.monitor.ui.cost

import androidx.lifecycle.MutableLiveData
import com.syqc.comlib.utils.GsonUtil
import com.syqc.entity.InsureInfoFee
import com.syqc.monitor.R
import com.syqc.utils.TimeUtil
import com.syqc.utils.ToolsUtil

class InsureViewModel : BaseFeeViewModel() {
    //保险公司
    var insureCom = ""

    //保险单号
    var insureNum = ""

    //加油日期
    var update = TimeUtil.instance.getNowTime(true)

    //开始时间
    var startDate = update

    //结束时间
    var endDate = update

    //商业险
    var business = ""
        set(value) {
            field = value
            insureTotal()
        }

    //交强险
    var strong = ""
        set(value) {
            field = value
            insureTotal()
        }

    val feeTotalLive = MutableLiveData<String>()

    private var insureFeeEntity: String = ""
    private var insureFee: String = ""

    /** 油量合计 */
    private fun insureTotal() {
        feeTotal = getDouble(business) + getDouble(strong)
        feeTotalLive.postValue(ToolsUtil.formatTo2F(feeTotal))
    }

    override fun isParamsOk(): Boolean {
        if (insureCom.isEmpty()) {
            toast(R.string.hint_toast_insure_company)
            return false
        }
        if (insureNum.isEmpty()) {
            toast(R.string.hint_toast_insure)
            return false
        }
        if (business.isEmpty() || strong.isEmpty()) {
            toast(R.string.hint_toast_fee)
            return false
        }
        if (endDate.replace("-", "").toLong() <= startDate.replace("-", "").toLong()) {
            toast(R.string.toast_start_less_than_end_time_date)
            return false
        }
        val entity = InsureInfoFee(
            carId, update, remark, ToolsUtil.formatTo2F(feeTotal),
            endDate, insureNum, insureCom, strong, startDate, business
        )

        insureFee = GsonUtil.toJson(entity)
        if ("$insureFee$photos" == insureFeeEntity && isSuccess) {
            toast(R.string.toast_repeat_same_data)
            return false
        }
        insureFeeEntity = "$insureFee$photos"
        return true
    }

    fun uploadInsure() {
        if (!isParamsOk()) return
        uploadFee("FeeInsure", insureFee.replace("%","%25"))
    }

    override fun clearData() {
        super.clearData()
        update = TimeUtil.instance.getNowTime(true)
        startDate = update
        endDate = update
    }
}